package com.aismy.admin.utils;

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author Aismy
 * @date 2025/03/16
 **/
@Component
public class AiUtils {
    @Value("${ai.apiKey}")
    private String apiKey;
    @Value("${ai.model}")
    private String model;
    @Value("${ai.baseUrl}")
    private String baseUrl;

    public Optional<String> sendMessage(String message) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
                .apiKey(apiKey)
                .baseUrl(baseUrl)
                .build();
        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .addUserMessage(message)
                .model(model)
                .build();
        ChatCompletion chatCompletion = client.chat().completions().create(params);
       return chatCompletion.choices().get(0).message().content();
    }

}
